<?php

// no direct access
defined( '_VALID_MOS' ) or die( 'Restricted access' );

$_MAMBOTS->registerFunction( 'onAfterStart', 'DecryptResource' );

function DecryptResource( ) {
    global $database, $_MAMBOTS, $mainframe, $option, $task, $Itemid;

    $my = $mainframe->getUser();

    $decodedURL = mosGetParam($_REQUEST,'resource','');
    
    if ($decodedURL == ''){
        return;
    }

    if ($my->id){
        $decodedURL = base64_decode($decodedURL);
        $possibleID = substr($decodedURL,0,strlen($mainframe->_session->session_id));
        if ( $possibleID == $mainframe->_session->session_id){

            $query = "SELECT id"
            . "\n FROM #__menu"
            . "\n WHERE link LIKE 'index.php?option=com_docman%'"
            ;
            $database->setQuery( $query );
            $menus = $database->loadObjectlist();

            $decodedURL = substr($decodedURL,strlen($mainframe->_session->session_id));
            $decodedURL = base64_decode($decodedURL);
            $regex = '/document=([0-9]+)/i'; 
            preg_match_all( $regex, $decodedURL, $matches );
            $decodedURL = $matches[1][0];
            $option = $_REQUEST['option'] = $_GET['option'] = $_POST['option'] = 'com_logs';
            //$task = $_REQUEST['task'] = $_GET['task'] = $_POST['task'] = 'doc_download';
            $Itemid = $_REQUEST['Itemid'] = $_GET['Itemid'] = $_POST['Itemid'] = @$menus[0]->id;
            $_REQUEST['gid'] = $_GET['gid'] = $_POST['gid'] = $decodedURL;
            //$_REQUEST['no_html'] = $_GET['no_html'] = $_POST['no_html'] = 1;

            $mainframe->_setAdminPaths( $option, $mainframe->getCfg( 'absolute_path' ) );
        }
    }
    

    return true;

}
?>